package com.skymxc.wandroidtodo.data.net;

import com.skymxc.wandroidtodo.entity.NetResponse;
import com.skymxc.wandroidtodo.entity.PageResult;
import com.skymxc.wandroidtodo.entity.TODOItem;
import com.skymxc.wandroidtodo.entity.UserInfo;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * <p>
 *
 * </p>
 *
 * @author 孟祥超
 * <p>
 * date: 2021/12/1  7:26 上午
 */
public class APIManager {

    private static class APIManagerHolder {
        private static final APIManager INSTANCE = new APIManager();
    }

    public static APIManager getInstance() {
        return APIManagerHolder.INSTANCE;
    }

    private APIManager() {
        this.apiService = RetrofitManager.getManager().getRetrofit().create(APIService.class);
    }

    private APIService apiService;

    public Observable<UserInfo> login(String username, String password) {
        return apiService.login(username, password)
                .flatMap(new NetResponseFunction<UserInfo>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<UserInfo> register(String username, String password, String repassword) {
        return apiService.register(username, password, repassword)
                .flatMap(new NetResponseFunction<UserInfo>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<PageResult> pageList(int page) {
        return apiService.pageList(page)
                .flatMap(new NetResponseFunction<>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<TODOItem> addItem(String title, String content, String date, int type, int priority) {
        return apiService.addItem(title, content, date, type, priority)
                .flatMap(new NetResponseFunction<>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<TODOItem> updateItem(long id, String title, String content, String date, int status) {
        return apiService.updateItem(id, title, content, date, status)
                .flatMap(new NetResponseFunction<>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<TODOItem> updateItemStatus(long id, int status) {
        return apiService.updateItemStatus(id, status)
                .flatMap(new NetResponseFunction<>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<NetResponse> deleteItem(long id){
        return apiService.deleteItem(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

}
